package cn.itcast.util;

import cn.itcast.pojo.temporaryOrder;
import com.sun.mail.util.MailSSLSocketFactory;
import lombok.extern.slf4j.Slf4j;
import sun.rmi.runtime.Log;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.*;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

/**
 * @author 辉
 * @title: SendEmail
 * @projectName shic
 * @description: TODO
 * @date 2019/12/1417:31
 */
@Slf4j
public class SendEmail {

    int verificationCode = (int) ((Math.random() * 9 + 1) * 100000);


    public int sendMail(List<String> receivers, temporaryOrder temporaryOrder) {


        List<File> fileList = new ArrayList<>();
        if (receivers != null && receivers.size() > 0) {
            log.debug("发送邮件");
            try {
                Properties properties = new Properties();
                //企业邮箱使用：smtp.exmail.qq.com
                properties.put("mail.smtp.host", "smtp.qq.com");
                properties.put("mail.smtp.port", "465");
                properties.put("mail.smtp.auth", "true");
                properties.put("mail.smtp.ssl.enable", "true");
                // socketfactory来取代默认的socketfactory
                properties.put("mail.smtp.socketFactory.fallback", "false"); // 只处理SSL的连接,对于非SSL的连接不做处理
                properties.put("mail.smtp.socketFactory.port", "465");
                properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");




                Session session = Session.getInstance(properties);
                MimeMessage message = new MimeMessage(session);
                // 发送邮件协议名称
                properties.setProperty("mail.transport.protocol", "smtp");

                /**SSL认证，注意腾讯邮箱是基于SSL加密的，所以需要开启才可以使用**/
                MailSSLSocketFactory sf = new MailSSLSocketFactory();
                sf.setTrustAllHosts(true);

                //设置是否使用ssl安全连接（一般都使用）
                properties.put("mail.smtp.ssl.enable", "true");
                properties.put("mail.smtp.ssl.socketFactory", sf);


                // 发件人
                message.setFrom(new InternetAddress("634272623@qq.com"));

                // 收件人(多个)
                InternetAddress[] sendTo = new InternetAddress[receivers.size()];
                for (int i = 0; i < receivers.size(); i++) {
                    sendTo[i] = new InternetAddress(receivers.get(i));
                }
                message.setRecipients(MimeMessage.RecipientType.TO, sendTo);

                // 邮件主题
//                message.setSubject("邮件主题")
                message.setSubject("B端货量提交推送", "UTF-8");
                // 添加邮件的各个部分内容，包括文本内容和附件
                Multipart multipart = new MimeMultipart();

                // 添加邮件正文
                BodyPart contentPart = new MimeBodyPart();
                //contentPart.setContent("您的诗词歌赋验证码为："+verificationCode, "text/html;charset=UTF-8");
                contentPart.setContent("回收人员："+temporaryOrder.getRecycle()+"，开发人员:"+temporaryOrder.getExploit()+",时间："+temporaryOrder.getDate()+",地点："+temporaryOrder.getAddress()+"。详细内容点击网址：https://www.dhnep.cn:8181/dhn/or/dist/sthb.html", "text/html;charset=UTF-8");
                multipart.addBodyPart(contentPart);

                // 遍历添加文件附件
                if (fileList != null && fileList.size() > 0) {
                    for (File file : fileList) {
                        BodyPart attachmentBodyPart = new MimeBodyPart();
                        DataSource source = new FileDataSource(file);
                        attachmentBodyPart.setDataHandler(new DataHandler(source));
                        attachmentBodyPart.setFileName(MimeUtility.encodeText(file.getName(), "UTF-8", "B"));
                        multipart.addBodyPart(attachmentBodyPart);
                    }
                }
                message.setContent(multipart);
                message.setSentDate(new Date());
                message.saveChanges();



                Transport transport = session.getTransport();
                //或者为企业邮箱和密码
                transport.connect("634272623@qq.com", "oyldldijvncibbbi");
                transport.sendMessage(message, message.getAllRecipients());
                transport.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return verificationCode;
    }
}
